﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib;
using System.Threading;
using WFNetLib.Log;
using WFNetLib.WinAPI;

namespace 邻苯浓度测量仪
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            serialPort1.ReadTimeout = 500;
            byte[] xx = new byte[4];
            xx[0] = 0x3f;
            xx[1] = 0x1a;
            xx[2] = 0x36;
            xx[3] = 0xe3;
            float ff=BitConverter.ToSingle(xx, 0);
            byte[] bbb = BitConverter.GetBytes(0.6024f);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "开始采集")
            {
                serialPort1.Close();
                serialPort1.PortName = cbCom.Text;
                if (!WFGlobal.OpenSerialPort(ref serialPort1, "所选"))
                {
                    return;
                }
                button1.Text = "停止采集";
                button2.Enabled = false;
                bComRead = true;
                Thread t = new Thread(ComProc_Read);
                t.Start(); 
            }
            else
            {
                bComRead = false;
                while (!bComReadExit) ;
                button1.Text = "开始采集";
                button2.Enabled = true;
                serialPort1.Close();                
            }
            Win32API.MessageBeep(APIConstant.MB_ICONEXCLAMATION);
        }
        bool bComRead=false;
        bool bComReadExit = true;
        private void timer1_Tick(object sender, EventArgs e)
        {
            ComParket.ComProc(ref serialPort1, 0x0001, null);
        }
        void ComProc_Read()
        {
            bComReadExit = false;
            int start = Environment.TickCount-100000;
            while (bComRead)
            {
                if (Math.Abs(Environment.TickCount - start) > 5000)
                {
                    start = Environment.TickCount;
                    ComParket ret=ComParket.ComProc(ref serialPort1, 0x000b, null);
                    if (ret == null)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            MessageBox.Show("无法从设备中采集到数据，请检查连接情况");
                            bComRead = false;
                            button1.Text = "开始采集";
                            button2.Enabled = true;
                            serialPort1.Close();
                        }));                        
                        break;
                    }
                    else
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            float x1, x2;
                            x1 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE);
                            x2 = ComParket.ComProc_Byte2Float(ret.Data, ComParketHead.HEAD_SIZE + 4);
                            textBox1.Text = x1.ToString("f4");
                            textBox2.Text = x2.ToString("f4");
                            string TxtDataFormat = "{0,-20}{1,-15}{2,-15}";
                            string log=string.Format(TxtDataFormat,DateTime.Now.ToString(),textBox1.Text,textBox2.Text);                            
                            TextLog.AddTextLog(log,System.Windows.Forms.Application.StartupPath + "\\ReadData.txt",false);
                        }));  
                    }                  
                    
                }
            }
            bComReadExit = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (button1.Text != "开始采集")
            {
                bComRead = false;
                while (!bComReadExit) ;
                serialPort1.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            serialPort1.PortName = cbCom.Text;
            Form2 f = new Form2(serialPort1.PortName);
            f.ShowDialog();
        }
    }
}
